<template>
  <list-card>
    <el-form ref="queryForm" slot="search" inline :model="queryParams" size="small">
      <el-form-item label="租户名称" prop="name">
        <el-input v-model="queryParams.name" clearable placeholder="请输入租户名称" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="租户类型" prop="type">
        <el-input v-model="queryParams.type" clearable placeholder="请输入租户类型" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="字典状态">
          <el-option v-for="item in localDict.tenant_status" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div slot="operate">
      <el-button v-has-permi="['system:tenant:add']" icon="el-icon-plus" plain size="mini" type="primary"
                 @click="handleAdd">新增
      </el-button>
      <el-button v-has-permi="['system:tenant:remove']" :disabled="multiple" icon="el-icon-delete" plain size="mini"
                 type="danger" @click="handleDelete">删除
      </el-button>
      <el-button v-has-permi="['system:tenant:export']" icon="el-icon-download" plain size="mini" type="warning"
                 @click="handleExport">导出
      </el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"/>
    </div>
    <el-table slot="content" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="租户编码" prop="code"/>
      <el-table-column align="center" label="租户名称" prop="name" show-overflow-tooltip/>
      <el-table-column label="租户类型" prop="type">
        <!--        <template slot-scope="scope">-->
        <!--          <router-link :to="'/system/dict-data/index/' + scope.row.dictId" class="link-type">-->
        <!--            <span>{{ scope.row.dictType }}</span>-->
        <!--          </router-link>-->
        <!--        </template>-->
      </el-table-column>
      <el-table-column align="center" label="状态" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="localDict.tenant_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" prop="createTime" width="180"/>
      <el-table-column align="center" class-name="small-padding fixed-width" fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button v-has-permi="['system:tenant:edit']" icon="el-icon-edit" size="mini" type="text"
                     @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button v-has-permi="['system:tenant:remove']" icon="el-icon-delete" size="mini" type="text"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="pageParam.total>0" slot="page" :limit.sync="pageParam.pageSize" :page.sync="pageParam.pageNum"
                :total="pageParam.total" @pagination="getList"/>
    <tenant-model ref="model" @ok="getList"/>
  </list-card>
</template>

<script>
import {getStaticData} from "@/api/common";
import {list, remove} from "@/api/base";
import TenantModel from "./components/ModelCard.vue";

export default {
  name: "Tenant",
  components: {TenantModel},
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      baseUrl: '/system/tenant/',
      // 遮罩层
      loading: true,
      dataList: [],
      pageParam: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      // 查询参数
      queryParams: {
        name: undefined,
        code: undefined,
        status: undefined
      },
      // 显示搜索条件
      showSearch: true,
      localDict: {
        tenant_status: []
      }
    };
  },
  created() {
    this.getLocalDict();
    this.resetQuery()
  },
  methods: {
    async getLocalDict() {
      await getStaticData('system-status').then(res => {
        this.localDict.tenant_status = res.data
      })
    },
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      list(this.baseUrl, {
        ...this.queryParams,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then(res => {
          this.dataList = res.rows;
          this.pageParam.total = res.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.model.add()
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.model.edit(row.id)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的租户？').then(() => {
        remove(this.baseUrl, ids, ids.length > 1).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/tenant/export', {
        ...this.queryParams
      }, `type_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
